package org.dormi.learn.product.port.persist.event;

import org.apache.commons.collections4.CollectionUtils;
import org.dormi.learn.event.BaseEvent;
import org.dormi.learn.event.EventStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 事件存储
 *
 * @author : dormi330
 * @version : 1.0
 */
@Component
public class EventStoreImpl implements EventStore {

    @Autowired private EventDAO eventDAO;

    @Override
    public void save(BaseEvent e) {
        BaseEventPO po = EventConverter.MAPPER.toPO(e);
        eventDAO.save(po);
    }

    @Override
    public void saveList(List<BaseEvent> events) {
        if (CollectionUtils.isEmpty(events)) {
            return;
        }

        events.forEach(e -> {
            eventDAO.save(EventConverter.MAPPER.toPO(e));
        });
    }
}
